﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Game_Admin_Demo.admin
{
    public partial class Games : System.Web.UI.Page
    {
        DataClasses1DataContext dc = new DataClasses1DataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var list = dc.sanphams.Join(dc.danhmucs, dm => dm.madm, sp => sp.madm, (sp, dm) => new
                {
                    masp = sp.masp,
                  name=sp.tensp,
                  cat=dm.tendm,
                  price = sp.gia,
                  status=sp.tinhtrang,
                  quantity = sp.soluong,
                });

                ListView1.DataSource = list;
                ListView1.DataBind();
                ViewState["direction"] = SortDirection.Ascending;
            }
        }

        protected void ListView1_Sorting(object sender, ListViewSortEventArgs e)
        {
            if (e.SortExpression == "PRICE")
            {
                if (e.SortDirection == (SortDirection)ViewState["direction"])
                {
                    var list = dc.sanphams.Join(dc.danhmucs, dm => dm.madm, sp => sp.madm, (sp, dm) => new
                    {
                        masp = sp.masp,
                        name = sp.tensp,
                        cat = dm.tendm,
                        price = sp.gia,
                        status = sp.tinhtrang,
                        quantity = sp.soluong,
                    }).OrderByDescending(z => z.price);

                    ListView1.DataSource = list;
                    ViewState["direction"] = SortDirection.Descending;
                    ListView1.DataBind();
                }

                else
                {
                    var list = dc.sanphams.Join(dc.danhmucs, dm => dm.madm, sp => sp.madm, (sp, dm) => new
                    {
                        masp = sp.masp,
                        name = sp.tensp,
                        cat = dm.tendm,
                        price = sp.gia,
                        status = sp.tinhtrang,
                        quantity = sp.soluong,
                    }).OrderBy(z => z.price);

                    ListView1.DataSource = list;
                    ViewState["direction"] = SortDirection.Ascending;
                    ListView1.DataBind();

                }

            }


        }

        protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
        {
            this.DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
            var list = dc.sanphams.Join(dc.danhmucs, dm => dm.madm, sp => sp.madm, (sp, dm) => new
            {   masp=sp.masp,
                name = sp.tensp,
                cat = dm.tendm,
                price = sp.gia,
                status = sp.tinhtrang,
                quantity = sp.soluong,
            });

            ListView1.DataSource = list;
            ListView1.DataBind();
        }

        protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                LinkButton lb = (LinkButton)e.Item.FindControl("LinkButton1");
                lb.Attributes.Add("onclick", "return confirm('sure')");
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            LinkButton lb = sender as LinkButton;
            HiddenField hf = lb.FindControl("HiddenField1") as HiddenField;
            int masp = Convert.ToInt32(hf.Value);
            var spz = dc.sanphams.SingleOrDefault(s=>s.masp==masp);
            dc.sanphams.DeleteOnSubmit(spz);
            dc.SubmitChanges();
            var list = dc.sanphams.Join(dc.danhmucs, dm => dm.madm, sp => sp.madm, (sp, dm) => new
            {
                masp = sp.masp,
                name = sp.tensp,
                cat = dm.tendm,
                price = sp.gia,
                status = sp.tinhtrang,
                quantity = sp.soluong,
            });

            ListView1.DataSource = list;
            ViewState["direction"] = SortDirection.Descending;
            ListView1.DataBind();
        }

       
    }
}